package com.atung.parking.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.annotation.Resource;

/**
 * @author wangbo
 * @since 2022/10/17 11:48
 */
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private JwtAuthenticationTokenFilterHandler jwtAuthenticationTokenFilterHandler;

    @Resource
    private AuthenticationEntryPoint authenticationEntryPoint;
    @Resource
    private LogoutSuccessHandler logoutSuccessHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

    //配置内存认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                //未登录认证访问接口处理
                .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
//                //无权限访问接口异常处理
//                .accessDeniedHandler(restfulAccessDeniedHandler)
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/api/auth/**").permitAll()
                .antMatchers("/api/fegin/**/**").permitAll()
                .antMatchers("/auth/**/**").permitAll()
                .antMatchers("/api/auth/**/**").permitAll()
                .antMatchers("/doc.html").permitAll()
                .antMatchers("/v3/**").permitAll()
                .antMatchers("/api/**/export").permitAll()
                .antMatchers("/swagger-ui/**").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                .antMatchers("/usr/local/public/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/api/file/**").permitAll()
                .antMatchers("/api/home/**").permitAll()
                .antMatchers("/*/api-docs").permitAll()
                .antMatchers("/order/callback").permitAll()
                .antMatchers("/parking/**/**/**").permitAll()
                .antMatchers("/api/order/callback").permitAll()
                .anyRequest().authenticated()
                .and().headers().cacheControl();
//        // 增加退出操作
//        http.logout()
//                .logoutUrl("/api/auth/logout")   // 退出操作的请求接口
//                //退出接口访问成功拦截业务
//                .logoutSuccessHandler(logoutSuccessHandler);
        //登录jwt拦截设置
        http.addFilterBefore(jwtAuthenticationTokenFilterHandler, UsernamePasswordAuthenticationFilter.class);


    }
}
